
package nccloud.web.arap.amortization.action.base;

import java.util.Map;
import nc.vo.pubapp.pattern.model.entity.bill.AbstractBill;
import nccloud.dto.mmbd.script.entity.BDScriptResultDTO;
import nc.vo.pubapp.pattern.tool.performance.DeepCloneTool;
import java.util.Iterator;
import java.util.List;
import java.util.ArrayList;
import java.util.Map.Entry;
import nc.vo.ml.NCLangRes4VoTransl;

public abstract class BaseScriptAction extends BaseBatchOperateAction {

        @Override
        public BaseBatchResult batchOperate(Object[] objs) {
                AbstractBill[] bills = (AbstractBill[]) ((AbstractBill[]) objs);
                DeepCloneTool tool = new DeepCloneTool();
                AbstractBill[] cloneBills = (AbstractBill[]) ((AbstractBill[]) tool.deepClone(bills));
                BDScriptResultDTO ret = this.execScript(cloneBills);
                BaseBatchResult result = this.convertToResult(ret, bills);
                return result;
        }
        
        public abstract Map<String, Object> processSuccessResult(AbstractBill[] var1, AbstractBill[] var2);

        public abstract BDScriptResultDTO execScript(AbstractBill[] var1);

        public BaseBatchResult convertToResult(BDScriptResultDTO dto, AbstractBill[] bills) {
                BaseScriptResult result = new BaseScriptResult();
                Object obj = dto.getData();
                if (obj != null && obj instanceof Map) {
                        result.setUserObj((Map) obj);
                } else if (bills.length == 1) {
                        AbstractBill bill = dto.getSucessVOs()[0];
                        result.setSuccessIds(new String[]{bill.getParent().getPrimaryKey()});
                        result.setSuccessResult(this.processSuccessResult(dto.getSucessVOs(), bills));
                } else {
                        Map<Integer, String> errMsgs = dto.getErrorMessageMap();
                        AbstractBill[] successVos = dto.getSucessVOs();
                        if (errMsgs != null && errMsgs.size() > 0) {
                                List<String> msgs = new ArrayList();
                                Iterator var8 = errMsgs.entrySet().iterator();

                                while (var8.hasNext()) {
                                        Entry<Integer, String> entry = (Entry) var8.next();
                                        msgs.add(NCLangRes4VoTransl.getNCLangRes().getStrByID("4009012_0", "04009012-0022", (String) null,
                                                        new String[]{String.valueOf((Integer) entry.getKey() + 1)}) + (String) entry.getValue());
                                }

                                result.setErrMsg((String[]) msgs.toArray(new String[0]));
                        }

                        if (successVos != null && successVos.length > 0) {
                                String[] successIds = new String[successVos.length];

                                for (int j = 0; j < successVos.length; ++j) {
                                        successIds[j] = successVos[j].getPrimaryKey();
                                }

                                result.setSuccessIds(successIds);
                                result.setSuccessResult(this.processSuccessResult(successVos, bills));
                        }
                }

                return result;
        }

}
